<?php
class Repost_model extends CI_Model {
    const REPOST_PER_PAGE = 15;

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    public function get_single($repost_id) {
        $this->db->from('repost');
        $this->db->where(array('repost_id' => $repost_id));
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->row_array();
        } else {
            return NULL;
        }
    }
    /**
     * Get raw repost data
     *
     * @access public
     * @param int $post_id id of the post
     * @param int $repost_id id of the last/offset repost [optional]
     * @param int $num number of reposts of a page [optional]
     * @return array
     */
     /*
    public function get($post_id, $repost_id = 0, $num = Repost_model::REPOST_PER_PAGE) {
        $this->db->select(
            'r.repost_id
            , r.post_id
            , r.repost_time
            , r.repost_content
            , r.level_no
            , u.user_id
			, u.gender
            , u.nickname
            , r2.repost_id as re_repost_id
            , r2.post_id as re_post_id
            , r2.repost_time as re_repost_time
            , r2.repost_content as re_repost_content
            , r2.level_no as re_level_no
            , u2.user_id as re_user_id
            , u2.nickname as re_nickname
			, u2.gender as re_gender');
        $this->db->from('repost r');
        $this->db->join('user u', 'u.user_id = r.user_id', 'left');
        $this->db->join('repost r2', 'r2.repost_id = r.re_repost_id', 'left');
        $this->db->join('user u2', 'u2.user_id = r2.user_id', 'left');
        $this->db->where('r.post_id', $post_id);
        
        // 分页
        if ($repost_id != 0) {
            $this->db->where('r.repost_id >', $repost_id);
        }
        
        $this->db->order_by('r.repost_time');
        $this->db->limit($num);
        $query = $this->db->get();
        
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }
    }
    */
    
    public function get($post_id, $pn = 1, $num = Repost_model::REPOST_PER_PAGE) {
        // 引入悄悄话
        $self = self_id();
        $sql = '
            SELECT r.repost_id
                , r.post_id
                , r.repost_time
                , r.repost_content
                , r.repost_private
                , r.level_no
                , u.user_id
                , u.gender
                , u.nickname
                , r2.repost_id as re_repost_id
                , r2.post_id as re_post_id
                , r2.repost_time as re_repost_time
                , r2.repost_content as re_repost_content
                , r2.repost_private as re_repost_private
                , r2.level_no as re_level_no
                , u2.user_id as re_user_id
                , u2.nickname as re_nickname
                , u2.gender as re_gender
                , p.user_id as post_user_id
            FROM repost r 
            LEFT JOIN user u ON u.user_id = r.user_id 
            LEFT JOIN repost r2 ON r2.repost_id = r.re_repost_id 
            LEFT JOIN user u2 ON u2.user_id = r2.user_id 
            LEFT JOIN post p ON p.post_id = r.post_id 
            WHERE (r.post_id = ? AND r.display = 1) 
                AND (r.repost_private = 0 
                    OR (r.repost_private = 1 AND r.user_id = ?) 
                    OR (r.repost_private = 1 AND u2.user_id = ?) 
                    OR (r.repost_private = 1 AND p.user_id = ?)
                )
            ORDER BY r.repost_time 
            LIMIT ?, ? ;';
        $query = $this->db->query($sql, array($post_id, $self, $self, $self, ($pn - 1) * $num, $num));
        
        /*
        $this->db->select(
            'r.repost_id
            , r.post_id
            , r.repost_time
            , r.repost_content
            , r.repost_private
            , r.level_no
            , u.user_id
			, u.gender
            , u.nickname
            , r2.repost_id as re_repost_id
            , r2.post_id as re_post_id
            , r2.repost_time as re_repost_time
            , r2.repost_content as re_repost_content
            , r2.repost_private as re_repost_private
            , r2.level_no as re_level_no
            , u2.user_id as re_user_id
            , u2.nickname as re_nickname
			, u2.gender as re_gender
            , p.user_id as post_user_id');
        $this->db->from('repost r');
        $this->db->join('user u', 'u.user_id = r.user_id', 'left');
        $this->db->join('repost r2', 'r2.repost_id = r.re_repost_id', 'left');
        $this->db->join('user u2', 'u2.user_id = r2.user_id', 'left');
        $this->db->join('post p', 'p.post_id = r.post_id');
        $this->db->where('r.post_id', $post_id);
        $this->db->where('r.display', true); // filter 'deleted' reposts
        $this->db->order_by('r.repost_time');
        $this->db->limit($num, ($pn - 1) * $num);
        $query = $this->db->get();
        */
        
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }        
    }
    
    public function insert($repost) {
        $this->db->insert('repost', $repost);
        $res = $this->db->affected_rows();
        if ($res > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    /**
     * Wrap raw $reposts_data into the form that can directly be used to pass to view.
     *
     * @access public
     * @param array $repost_data
     * @return array
     */
    public function wrap($reposts_data) {
        // is_lz
        $is_lz = true;
        if (!isset($_SESSION['userinfo'])) {
            $is_lz = false;
        }  
        
        $reposts = array();
        
        if (isset($reposts_data)) {
            foreach ($reposts_data as $repost_data) {
                $repost = array(
                    'repost_id' => $repost_data['repost_id'],
                    'post_id' => $repost_data['post_id'],
                    'repost_content' => htmlspecialchars_decode($repost_data['repost_content']),
                    'repost_time' => $repost_data['repost_time'],
                    'repost_private' => $repost_data['repost_private'],
                    'level_no' => $repost_data['level_no'],
                    'user_id' => $repost_data['user_id'],
                    'nickname' => $repost_data['nickname'],
					'gender' => $repost_data['gender'],
                    'is_lz' => 
                        ($is_lz) 
                            ? (
                                ($_SESSION['userinfo']['user_id'] == $repost_data['post_user_id']) ? true : false
                            )
                            : false
                        
                );
                if (isset($repost_data['re_repost_id'])) {
                    $re_repost = array(
                        'repost_id' => $repost_data['re_repost_id'],
                        'post_id' => $repost_data['re_post_id'],
                        'repost_content' => htmlspecialchars_decode($repost_data['re_repost_content']),
                        'repost_time' => $repost_data['re_repost_time'],
                        'repost_private' => $repost_data['re_repost_private'],
                        'level_no' => $repost_data['re_level_no'],
                        'user_id' => $repost_data['re_user_id'],
                        'nickname' => $repost_data['re_nickname'],
						'gender' => $repost_data['re_gender']
                    );
                    $repost['re_repost'] = $re_repost;
                } else {
                    $repost['re_repost'] = NULL;
                }
                $reposts[] = $repost;
            }
        } else {
            $reposts = NULL;
        }
        
        return $reposts;
    }
    
    public function is_end($post_id, $repost_id = 0) {
        $this->db->from('repost r');
        $this->db->where('r.post_id', $post_id);
        if ($repost_id != 0) {
            $this->db->where('r.level_no >', "(select r2.level_no from repost r2 where r2.repost_id = $repost_id)", false);
        }
        
        $count = $this->db->count_all_results();
        return $count;
    }
    
    /**
     * 删除回帖
     * 
     * @param $repost_id 回帖ID号
     * @parem $post_id 帖子ID号 [optional]
     * 楼主自行删除需要 $post_id, 后台管理删除无需该参数
     * @return TRUE/FALSE
     */
    public function delete($repost_id, $post_id = NULL) {
        $this->db->set('display', false);
        $this->db->where('repost_id', $repost_id);
        if ($post_id != NULL) {
            $this->db->where('post_id', $post_id);
        }
        $this->db->update('repost');
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }        
    }
}
?>